package base

import (
	"go_gvm_mini/rtda"
	"go_gvm_mini/rtda/heap"
)

func InitClass(thread *rtda.JThread, klass *heap.Klass) {
	klass.StartInit()
	scheduleClinit(thread, klass)
	initSuperClass(thread, klass)
}

func initSuperClass(thread *rtda.JThread, klass *heap.Klass) {
	if !klass.IsInterface() {
		superClass := klass.SuperClass()
		if superClass != nil && !superClass.InitStarted() {
			InitClass(thread, superClass)
		}
	}
}

func scheduleClinit(thread *rtda.JThread, klass *heap.Klass) {
	clinit := klass.GetClinitMethod()
	if clinit != nil {
		newJFrame := thread.NewFrame(clinit)
		thread.PushJFrame(newJFrame)
	}
}
